package cn.iocoder.yudao.position.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.position.entity.PositionDO;
import cn.iocoder.yudao.position.service.PositionService;
import cn.iocoder.yudao.position.vo.PositionPageReqVO;
import cn.iocoder.yudao.position.vo.PositionRespVO;
import cn.iocoder.yudao.position.vo.PositionSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 职位-信息")
@RestController
@RequestMapping("/position/")
@Validated
public class PositionController {

    @Resource
    private PositionService positionService;

    @PostMapping("/create")
    @Operation(summary = "创建职位-信息")
    //@PreAuthorize("@ss.hasPermission('position::create')")
    public CommonResult<Long> create(@Valid @RequestBody PositionSaveReqVO createReqVO) {
        return success(positionService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职位-信息")
    //@PreAuthorize("@ss.hasPermission('position::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody PositionSaveReqVO updateReqVO) {
        positionService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职位-信息")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('position::delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        positionService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职位-信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('position::query')")
    public CommonResult<PositionRespVO> get(@RequestParam("id") Long id) {
        PositionDO positionDO = positionService.get(id);
        return success(BeanUtils.toBean(positionDO, PositionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职位-信息分页")
    //@PreAuthorize("@ss.hasPermission('position::query')")
    public CommonResult<PageResult<PositionRespVO>> getPage(@Valid PositionPageReqVO pageReqVO) {
        PageResult<PositionDO> pageResult = positionService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PositionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职位-信息 Excel")
    //@PreAuthorize("@ss.hasPermission('position::export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid PositionPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PositionDO> list = positionService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职位-信息.xls", "数据", PositionRespVO.class, BeanUtils.toBean(list, PositionRespVO.class));
    }

}